package edu.cmu.cs.stage3.alice.authoringtool.util;

import javax.swing.filechooser.FileFilter;

/**
 * @author Jason Pratt
 */
public class ExtensionFileFilter extends FileFilter implements Comparable {
	private String extension;
	private String description;

	public ExtensionFileFilter( String extension, String description ) {
		this.extension = extension.toUpperCase();
		this.description = description;
	}

	//Rem @Override
	public boolean accept( java.io.File f ) {
		return f.isDirectory() || 
			getExtension(f).equalsIgnoreCase( extension ) || 
			getExtension(f).equalsIgnoreCase("a2w") ||
			getExtension(f).equalsIgnoreCase("xml");
	}

	//Rem @Override
	public String getDescription() {
		return description;
	}

	public String getExtension() {
		return extension;
	}

	public static String getExtension( java.io.File f ) {
		String ext = "";
		String fullName = f.getName();
		int i = fullName.lastIndexOf( '.' );

		if( (i > 0) && (i < fullName.length() - 1) ) {
			ext = fullName.substring( i+1 ).toUpperCase();
		}
		return ext;
	}

	public int compareTo( Object o ) {
		if( o instanceof ExtensionFileFilter ) {
			return description.compareTo( ((ExtensionFileFilter)o).getDescription() );
		}
		return description.compareTo( o.toString() );
	}
}
